package com.blb.permission_ms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.permission_ms.entity.Permission;
import com.blb.permission_ms.entity.Role;
import com.blb.permission_ms.entity.User;
import com.blb.permission_ms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Security需要的用户登录逻辑实现类
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserService userService;

    /**
     * 通过用户名查找用户
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //要通过用户名查找用户
        User user = userService.getOne(new QueryWrapper<User>().lambda().eq(User::getUsername, username));
        if(user == null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        //查询所有权限
        List<GrantedAuthority> authorities = userService.getAuthoritiesByUsername(username);
        org.springframework.security.core.userdetails.User userDetails =
                new org.springframework.security.core.userdetails.User(username,user.getPassword(),authorities);
        return userDetails;
    }
}
